<template>
  <div class="home">
    <div
      class="left-home"
      :style="isFold ? { width: '64px' } : { width: '200px' }"
    >
      <div
        class="box-left"
        :style="isFold ? { width: '64px' } : { width: '200px' }"
      >
        <p>
          <img :src="leftImg" alt="" />
          <span v-if="!isFold">管理后台</span>
        </p>
        <div
          class="pullDown-box"
          :style="isFold ? { left: '6px' } : { left: '20px' }"
          @mouseleave="isOpenChange(false)"
        >
          <button
            type="primary"
            :style="isFold ? { width: '50px' } : { width: '160px' }"
            @mouseenter="isOpenChange(true)"
          >
            <span class="add">+</span>
            <span v-if="!isFold">新建</span>
          </button>
          <div class="box" v-if="isOpen">
            <p @click="toAbout()">新建文章-协同编辑器</p>
            <p @click="toEditor()">新建文章</p>
            <p @click="toEditor()">新建页面</p>
          </div>
        </div>
      </div>
      <el-row class="tac">
        <el-col :span="12">
          <el-menu
            default-active="2"
            class="el-menu-vertical-demo"
            background-color="#001529"
            text-color="#fff"
            active-text-color="#ffd04b"
            :router="true"
            :collapse="isFold"
            :unique-opened="true"
            :collapse-transition="false"
          >
            <!-- 左侧列表根据routes返回的结果渲染 -->
            <div v-for="(item, ind) in routes" :key="ind">
              <el-submenu index="1" v-if="item.children">
                <template slot="title">
                  <i :class="item.icon"></i>
                  <span v-if="!isFold">{{ item.name }}</span>
                </template>
                <el-menu-item
                  v-for="(item1, ind1) in item.children"
                  :key="ind1"
                  :index="item1.path"
                >
                  <i :class="item1.icon"></i>
                  <span slot="title">{{ item1.name }}</span>
                </el-menu-item>
              </el-submenu>

              <el-menu-item
                :index="item.path"
                v-if="!item.children && !item.redirect"
              >
                <i :class="item.icon"></i>
                <span slot="title">{{ item.name }}</span>
              </el-menu-item>
            </div>
          </el-menu>
        </el-col>
      </el-row>
    </div>
    <!-- 右边显示的 -->
    <div class="right-home">
      <Header></Header>
      <router-view></router-view>
    </div>
  </div>
</template>
<script>
import left from "../../assets/left.png";
// 引入全部路由表
import allRoutes from "../../router/allRoutes";
// 引入过滤路由的方法
import { filterRoutes } from "../../utils/filterRouter";
import Header from "./components/Header.vue";
export default {
  data() {
    return {
      leftImg: left,
      isOpen: false,
    };
  },
  computed: {
    isFold() {
      return this.$store.state.home.isFold;
    },
    // 刷新的时候重新获取要渲染的路由表
    routes() {
      // 从本地获取用户信息，如果没用存储则赋值为空
      let userInfo = JSON.parse(window.localStorage.getItem("userInfo")) || "";
      // 判断用户信息是否为空
      if (userInfo) {
        // 把当前用户职位传递过去
        let infoRouter = filterRoutes(userInfo.role, allRoutes);
        return infoRouter;
      } else {
        // 为空则跳转到登陆页面
        this.$router.push("/login");
        return [];
      }
    },
  },
  methods: {
    isOpenChange(boolean) {
      this.isOpen = boolean;
    },
    toEditor() {
      this.$router.push("/editor");
    },
    toAbout(){
      this.$router.push("/about");
    }
  },
  components: {
    Header,
  },
};
</script>
<style lang="scss" scoped>
.home {
  width: 100%;
  height: 100%;
  display: flex;
  overflow: hidden;
  .left-home {
    height: 100%;
    background-color: #001529;
    .box-left {
      height: 100px;
      color: #fff;
      display: flex;
      flex-direction: column;
      align-items: center;
      // border-right: 1px solid #fff;
      position: relative;
      > p {
        display: flex;
        align-items: center;
        margin: 15px 0;

        img {
          width: 30px;
          height: 30px;
          margin-right: 5px;
        }
        span {
          font-size: 20px;
        }
      }
      .pullDown-box {
        width: 160px;
        // border: 1px solid #ccc;
        position: absolute;
        top: 60px;
        z-index: 99;
        button {
          height: 40px;
          background-color: #0188fb;
          color: #fff;
          border: none;
          outline: none;
          span {
            height: 30px;
            font-size: 18px;
          }
        }
        .box {
          width: 160px;
          background-color: #fff;

          p {
            width: 100%;
            height: 30px;
            line-height: 30px;
            margin-bottom: 3px;
            color: #000;
            padding: 0 10px;
            font-size: 14px;
          }
          p:hover {
            background-color: #ccc;
          }
        }
      }
    }
    .tac {
      width: 100%;
      .el-col {
        width: 100%;
        .el-menu-item {
          width: 100%;
          height: 40px;
          line-height: 40px;
        }
      }
    }
  }
  .right-home {
    flex: 1;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
  }
}
</style>